<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Shop_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    /**
     * @param $id
     * @return mixed
     * 查询单条
     */
    public function get_shop_user_by_id($id){
        $query = $this->db
            ->select('*')
            ->get_where('shop_users', array('shop_id'=>$id));
        return $query->row_array();
    }

    public function get_shop_user_by_uid($uid){
        $query = $this->db
            ->select('*')
            ->get_where('shop_users', array('user_id'=>$uid));
        return $query->row_array();
    }

    /**
     * @param $shop_id
     * @param $data
     * @return bool
     * 修改商家信息
     */
    public function sava_shop_user($shop_id,$data){

        $this->db->where('shop_id',$shop_id);
        $this->db->update('shop_users', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * @param $data
     * @return bool
     * 新增商家
     */
    public function add_shop_user($data){

        $this->db->insert('shop_users',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    /**
     * @return mixed
     * 查询所有普通商家
     */
    public function get_all_shop_users(){
        $this->db->select('*');
        $this->db->where('self_support',0);
        $this->db->from('shop_users');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    //查询所有的平台自营商家
    public function get_all_shop_self_users(){
        $this->db->select('*');
        $this->db->where('self_support',1);
        $this->db->from('shop_users');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    /**
     * 获取启用的规则
     */
    public function get_shop_users_by($map = "status=1"){
        $sql = "SELECT * FROM {$this->db->dbprefix}shop_users  where $map ORDER BY o asc";
        $query=$this->db->query($sql);
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    /**
     * @param $where
     * @return bool
     * 删除商家
     */
    public function delete_shop_users_by($where){
        $this->db->where($where)->delete('shop_users');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    //搜索商家
    public function get_all_shop_users_serch($keywords,$order){
        $query = $this->db->select('*');
        $this->db->from('shop_users');
        $this->db->like('shop_title', $keywords, 'both');
        $this->db->order_by('shop_time',$order);
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    //查询全部所有的商家
    public function get_all_shops(){
        $query = $this->db->select('*')->from('shop_users')->get();
        if($query->num_rows()>0){
            return $query->result_array();
        }
    }
    //删除商家
    public function del_shop_user_by_id($id){
        $this->db->where('shop_id',$id)->delete('shop_users');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

}
